package com.hushao.aimi.controller;

import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.entity.AimiProduct;
import com.hushao.aimi.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.sql.Array;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @description: 商品信息操作
 * @author: 胡少
 * @create: 2018-06-06 15:26
 **/
@Api(value = "商品操作接口")
@RestController
@Slf4j
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "添加商品信息")
    @PostMapping
    public ServerResponse addProduct(AimiProduct product)throws Exception{
        return productService.addProduct(product);

    }

    @ApiOperation(value = "商品状态修改")
    @PutMapping("/status/{productId}")
    public ServerResponse updateProductStatus(@PathVariable("productId")int productId,Integer status)throws Exception{
        return productService.updateProductStatus(productId,status);

    }

    /**
     * 获取商品详细
     * @param productId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取商品详细")
    @GetMapping("/{productId}")
    public ServerResponse detail(@PathVariable("productId") String productId)throws Exception{
        return productService.productDetail(productId);
    }


    /**
     * 获取商品列表（-分页）
     * @param name
     * @param pageable
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取商品列表")
    @GetMapping
    public ServerResponse getProductByNameLike(@RequestParam(value = "name",required = false,defaultValue = "")String name,
                                               Pageable pageable)throws Exception{
        return productService.getProductByNameLike(name,pageable);
    }

}
